﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;

namespace LZQXT.Test.Forms.DB
{
    public partial class FDBInit_Add : Form
    {
        public int i_Typ = 0;   //1.add; 2.edit;
        public string s_ID = "";

        public FDBInit_Add()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strSQL = "";

            //add
            if (i_Typ == 1)
            {
                strSQL = "SELECT * FROM T_DBCon WHERE [Server] LIKE '" + tboxServer.Text + "' AND [DataBase] LIKE '" + tboxDB.Text + "'";
                DataSet ds0 = FDBTree.fdbinit.dbCon.sel1(strSQL);
                if (!CDB.dsIsNull(ds0))
                {
                    MessageBox.Show("数据库配置已存在");
                    return;
                }

                DataSet ds1 = FDBTree.fdbinit.dbCon.sel1("SELECT MAX(ID) FROM T_DBCon");
                if (CDB.dsIsNull(ds1)) s_ID = "1";
                else s_ID = "" + (Convert.ToInt16(ds1.Tables[0].Rows[0][0].ToString()) + 1);

                strSQL = "INSERT INTO T_DBCon(ID,[Server],[User],[Pass],[DataBase]) VALUES(" + s_ID + ",'"
                   + tboxServer.Text + "','"
                   + tboxUser.Text + "','" + tboxPass.Text + "','" + tboxDB.Text + "' ) ";
            }

            //edit
            if (i_Typ == 2)
            {
                strSQL = "SELECT * FROM T_DBCon WHERE ID=" + s_ID;
                DataSet ds0 = FDBTree.fdbinit.dbCon.sel1(strSQL);
                if (CDB.dsIsNull(ds0))
                {
                    MessageBox.Show("数据库配置不存在");
                    return;
                }

                strSQL = "UPDATE T_DBCon Set [Server]='"
                    + tboxServer.Text + "', [User]='" + tboxUser.Text + "',[Pass]='" + tboxPass.Text
                    + "',[DataBase]='" + tboxDB.Text + "'"
                    + "WHERE ID=" + s_ID;
            }

            CDebug.Send(strSQL);
            FDBTree.fdbinit.dbCon.cmd1(strSQL);

            tboxDB.Text = "";

            this.Hide();
            FDBTree.fdbinit.Show();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            FDBTree.fdbinit.Show();
        }

        private void FDBInit_Add_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            FDBTree.fdbinit.Show();
        }
    }
}
